/*设置sidebar信息*/
(function(){
	var $sidebar = $('.main-sidebar');
	if($sidebar){
		var currentFirst = $sidebar.attr('data-current-first'),
		    currentSecond = $sidebar.attr('data-current-second');
		if(currentFirst && currentSecond){
			var sidebarMenuLis = $('.sidebar-menu li'),
			    treeviewMenuLis = $('.treeview-menu li');
			if(sidebarMenuLis){
				sidebarMenuLis.removeClass('active');
				
				$.each(sidebarMenuLis, function(index, element){
					var $element = $(element);
					if($element.attr('data-name')==currentFirst){
						$element.addClass('active');
					}
				});
			}
			
			if(treeviewMenuLis){
				treeviewMenuLis.removeClass('active');
				
				$.each(treeviewMenuLis, function(index, element){
					var $element = $(element);
					if($element.attr('data-name')==currentSecond){
						$element.addClass('active');
					}
				});
			}
		}
	}
})();

function formSubmit(options){
	options = options || {};
	options.url = options.url || 'add.jspx';
	options.form = options.form || '#form-data';
	
	$(options.form).validate({
		submitHandler: function(f){
			$.post(url, $(f).serialize(), function() {
				customGrid.reload(options);
				if(options.callback){
					callback(options);
				}
			});
		}
	});
}

function formAjaxSubmit(options){
	options = options || {};
	options.url = options.url || 'add.jspx';
	options.form = options.form || '#form-data';
	//$('.submit-button').on('click', function(){
		$(options.form).validate({
			submitHandler: function(f){
				$.post(options.url, $(f).serialize(), function(result) {
					if(result){
						if(result.success==0){
							layer.alert(result.message);
						}else{
							customGrid.reloadSelf();
							if(options.callback){
								options.callback(options);
							}
						}
						
					}else{
						layer.alert('表单提交出错');
					}
					
				});
				return false;
			}
		});
	//});
}
//增加提交
function addSubmit(){
	
	formAjaxSubmit({
		url: 'addJson.json',
		form: '#add-form-data',
		callback: hideAddModal
	});
}
//隐藏增加模态框
function hideAddModal(){
	$('#add-modal').modal('hide');
}
//显示修改模态框
function showEditModal(dataName){
	$('#edit-modal').on('show.bs.modal', function(e){
		var source = e.relatedTarget;
		if(source){
			var url = $(source).attr('data-url');
			if(url){
				$.get(url, function(result){
					var $content = $('#edit-modal .modal-content');
					$content.empty();
					var tpl = $('#edit-form-template').html();
					laytpl(tpl).render(result[dataName], function(render){
						$content.html(render);
						editSubmit();
					});
				});
			}
		}
	});
}
//修改提交
function editSubmit(){
	formAjaxSubmit({
		url: 'editJson.json',
		form: '#edit-form-data',
		callback: hideEditModal
	});
}
//隐藏修改模态框
function hideEditModal(){
	$('#edit-modal').modal('hide');
}